package com.study.order_service.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.study.order_service.domain.ProductOrder;
import com.study.order_service.client.ProductClinent;
import com.study.order_service.service.ProductOrderService;
import com.study.order_service.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: LWM
 * @date: 2019/2/20
 * @time: 11:48
 * Description: No Description
 */
@Service
@Slf4j
public class ProductOrderServiceImpl implements ProductOrderService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ProductClinent productClinent;

    @Override
    public ProductOrder save(int userId, int productId) {
        //调用riboon跨服务获取数据
      /*  Object object = restTemplate.getForObject("http://product-service/api/v1/product/find?id="+productId, Object.class);
        System.out.println(object);*/

        //调用feign
        String content = productClinent.findById(productId);
        //使用json解析
        JsonNode jsonNode = JsonUtil.str2JsonNote(content);
        log.info("save");

        //获取商品详情
        ProductOrder productOrder = new ProductOrder();
        productOrder.setCreateTime(new Date());
        productOrder.setTradeNo(UUID.randomUUID().toString());
        productOrder.setUserId(userId);
        productOrder.setProductName(jsonNode.get("name").toString());
        productOrder.setPrice(jsonNode.get("price").asInt());
        return productOrder;
    }
}
